//
//  DKBillsChannesViewController.m
//  DanKU
//
//  Created by EDY on 2023/11/10.
//

#import "DKBillsChannesViewController.h"
#import "DKRepayChannelsView.h"
#import "DKRepayAgreementFootView.h"
#import "DKOrderDetailsCell.h"
#import "DKOrderDetailModel.h"

@interface DKBillsChannesViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)DKBankimagesModel *imageModelList;

@property (nonatomic,strong)DKDelayFlexibleModel *extensionDetails;

@property(nonatomic,strong)DKOrderDetailModel *RepaySoure;

@end

@implementation DKBillsChannesViewController

-(void)setDataSoure:(DKOrderDetailModel *)dataSoure{
    _dataSoure = dataSoure;
    [self.tableView reloadData];
}

-(void)setDkRepaymentMethod:(DKRepaymentMethod)dkRepaymentMethod{
    _dkRepaymentMethod =dkRepaymentMethod;
}

-(void)viewWillAppear:(BOOL)animated{
    [self makeNavBack];
}

-(void)reportTotal:(NSString*)string{
    [DKPublicHttp apiUserBrhavRecordForAPI:@{@"loanOrderId":[NSString stringWithFormat:@"%@",self.dataSoure.orderNo]} withPageName:string withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  DK_Hex(@"#FAFAFA");
    [self creatUI];
    
    if (self.dkRepaymentMethod == Extension) {// 展期还款详情
         [DKPublicHttp apiGetOrderCalculateDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",self.dataSoure.orderNo],@"delayTerm":@"0",@"requestType":@"0"} complete:^(DKDelayFlexibleModel * _Nonnull orderlist) {
             self.extensionDetails = orderlist;
             [self.tableView reloadData];
             [self reportTotal:DK_DelayRepayment];
         }];
    }else{// 结清还款
        [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",self.dataSoure.orderNo],@"pageType":@"2"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
            
            self.RepaySoure = orderlist;
            NSLog(@"-----%@",self.RepaySoure);
            [self.tableView reloadData];
            [self reportTotal:DK_TotalRepayment];
        }];
    }
    

    // 银行卡列表
    [DKPublicHttp apiGetOrderBackImgAPI:@{} complete:^(DKBankimagesModel * _Nonnull orderlist) {
        self.imageModelList = orderlist;
        [self.tableView reloadData];
    }];
}

-(void)creatUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(DK_SafeAreaNav_H);
    }];
}

-(UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = DK_Hex(@"#FAFAFA");
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[DKOrderDetailsCell class] forCellReuseIdentifier:@"DKOrderDetailsCell"];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dkRepaymentMethod == Extension) {// 展期还款
        return self.extensionDetails.extensionDetails.count;
    }else{
        return self.RepaySoure.pairs.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DKOrderDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DKOrderDetailsCell" forIndexPath:indexPath];
    if (self.dkRepaymentMethod == Extension) {// 展期还款
        cell.extensionSoure = self.extensionDetails.extensionDetails[indexPath.row];
    }else{// 正常
        cell.PairsSoure = self.RepaySoure.pairs[indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 530+ (DK_SCREEN_W-60)/6-40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    DKRepayChannelsView *headView = [[DKRepayChannelsView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W ,530+ (DK_SCREEN_W-60)/6-40)];
    headView.imageListSoure = self.imageModelList;
    headView.dataSoure = self.dataSoure;
    if (self.dkRepaymentMethod == Extension) {// 展期还款
        headView.dkRepayChannelsType = Pay;
        headView.extenceSoure = self.extensionDetails;
    }else{
        headView.dkRepayChannelsType = Repay;
    }
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    DKRepayAgreementFootView *headView = [[DKRepayAgreementFootView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W , 100)];
    headView.Type = 0;
    return headView;
}


@end

